<template>
  <div class="tw-libname">
    <div class="tw-filterpane">
      <div class="tw-tagfilter"
        v-for="(parameter, index) in parameters"
        :key="index"
        :class="{xopen: open || !isFold}">
          <label v-if="parameter.label"
            class="tw-tagfilter-label"
            :style="{width: labelWidth}">{{parameter.label}}:</label>
          <div ref="tag-box"
            class="tw-tagfilter-tags">
            <a class="tw-tag xfilter"
              :class="{'xactive': values[index] === tag.value}"
              v-for="tag in parameter.items"
              :key="tag.value"
              @click="clickTag(tag, index)">
              <span>{{tag[dataMap.text]}}</span>
            </a>
            <a v-if="isFold && showToggleBtn[index]"
              class="tw-ico xtftoggle"
              :class="{'xopen': open, 'el-icon-arrow-down': open, 'el-icon-arrow-up': !open}"
              @click="open=!open"></a>
          </div>
      </div>
      <div class="tw-filterpane-toolbar">
        <a class="tw-btn xmain" @click="renderReport">查询</a>
        <a class="tw-btn xweaking" @click="renderReset">重置</a>
      </div>
    </div>
  </div>
</template>

<script>
import props from './props'

export default {
  name: 'web-menu-screen',

  props,
  model: {
    prop: 'value',
    event: 'change'
  },
  data () {
    return {
      dataMap: {
        text: 'label',
        value: 'value'
      },
      values: [], // 选中的数组
      open: false,
      showToggleBtn: [],
      isFold: true, // 是否需要收起展开
      toggleHeight: 50
    }
  },
  methods: {
    setStyle () {
      const style = `
        .tw-filterpane {
          background-color: ${this.filterBgColor};
        }
        .tw-tagfilter {
          position: relative;
          display: flex;
          align-items: flex-start;
          height: calc(${this.filterMargin} * 4 + ${this.filterHeight});
          padding: ${this.filterMargin} 0;
          overflow: hidden;
        }
        .tw-tagfilter-label {
          width: 5em;
          line-height: ${this.filterHeight};
          margin: ${this.filterMargin};
          font-weight: 700;
          color: ${this.labelColor};
          text-align: right;
        }
        .tw-tagfilter-tags > .tw-tag {
          font-size: ${this.fontSize};
          vertical-align: top;
        }
        .tw-tagfilter-tags > .tw-ico.xtftoggle {
          position: absolute;
          top: calc(${this.filterMargin} * 2);
          right: 0;
        }
        .tw-tag.xfilter {
          height: ${this.filterHeight};
          line-height: ${this.filterHeight};
          margin: ${this.filterMargin};
          border-radius: ${this.borderRadius};
        }
        .tw-tag {
          display: inline-block;
          height: 24px;
          line-height: 24px;
          padding: 0 10px;
          color: ${this.color};
          border: 1px solid ${this.borderColor};
          border-color: ${this.borderColor};
          font-style: normal;
          text-align: center;
          border-radius: 2px;
        }
        .tw-tagfilter.xobvious .tw-tag.xfilter {
          color: ${this.color};
          border-color: ${this.borderColor};
          font-size: ${this.fontSize};
        }
        .tw-tagfilter.xobvious .tw-tag.xactive{
          position: relative;
          color: ${this.chooseColor};
          border-color: ${this.chooseBgColor};
        }
        .tw-filterpane.xborder{
          border: 1px solid ${this.filterBorderColor};
        }
        .tw-filterpane.xbg{
          background: ${this.filterBgColor};
        }
        .tw-filterpane > .tw-tagfilter {
          border-bottom: 1px dashed ${this.filterBorderColor};
        }
        .tw-tag.xfilter:hover {
          color: ${this.chooseColor};
          background: ${this.chooseBgColor};
        }
        .tw-tag.xfilter.xactive {
          color: ${this.chooseColor};
          background: ${this.chooseBgColor};
        }
        .xtftoggle {
          font-size: 24px;
        }
        .tw-ico.xtftoggle:hover {
          color: ${this.chooseBgColor}
        }
        .tw-btn.xmain {
          color: ${this.xmainColor};
          background: ${this.xmainBgColor};
          border-color: ${this.xmainBgColor};
        }
        .tw-btn.xmain:hover {
          background: ${this.xmainFoucsColor}
        }
        .tw-btn.xweaking {
          color: ${this.xweakingColor};
          background: ${this.xweakingBgColor};
          border-color: #ccc;
        }
        .tw-btn.xweaking:hover {
          color: ${this.xweakingFoucsColor};
          background: ${this.xweakingBgColor};
          border-color: ${this.xweakingFoucsColor};
        }
      `
      const styleEl = document.createElement('style')
      styleEl.innerText = style.replace(/\n/g, '')

      document.head.appendChild(styleEl)
    },
    /**
     * 功能: 初始化是否显示切换按钮及选中状态等
     */
    init () {
      this.$nextTick(() => {

        if (this.parameters && this.parameters.length > 0) {
          this.parameters.forEach((item, index) => {
            this.values.push('')
            this.showToggleBtn.push(false)
            if ((this.$refs['tag-box'])[index].offsetHeight > this.toggleHeight) {
              this.showToggleBtn[index] = true
            }
          })
        }
      })
    },

    /**
     * 功能: 点击标签处理
     * @param {Object} tag 标签对应的数据对象
     */
    clickTag (tag,index) {
      // if (this.value === tag[this.dataMap.value]) {
      //   if (this.toggleSelf) {
      //     this.$emit('change', '')
      //   }
      // } else {
      //   this.$emit('change', tag[this.dataMap.value])
      // }
      this.$emit('change', tag[this.dataMap.value])
      if (this.values.length > 0) {
        this.values.splice(index, 1, tag.value)
      }
    },

    // 点击查询按钮
    renderReport () {
      this.$emit('toSearch', this.values)
    },
    // 点击重置按钮
    renderReset () {
      this.values.splice(0, this.parameters.length, '')
    }
  },

  computed: {
    style () {
      return {
        filterMargin: this.filterMargin,
        filterHeight: this.filterHeight,
        filterBgColor: this.filterBgColor,
        filterBorderColor: this.filterBorderColor,
        labelColor: this.labelColor,
        fontSize: this.fontSize,
        color: this.color,
        borderColor: this.borderColor,
        chooseBgColor: this.chooseBgColor,
        chooseColor: this.chooseColor,
        borderRadius: this.borderRadius,
        labelWidth: this.labelWidth,
        xmainColor: this.xmainColor,
        xweakingColor: this.xweakingColor,
        xmainFoucsColor: this.xmainFoucsColor,
        xweakingFoucsColor: this.xweakingFoucsColor,
        xmainBgColor: this.xmainBgColor,
        xweakingBgColor: this.xweakingBgColor
      }
    }
  },
  mounted () {
    window.aaa = this
    this.setStyle()
    this.init()
  },

  watch: {
    style: {
      deep: true,
      handler () {
        this.setStyle()
      }
    },
    parameters () {
      this.init()
    }
  },
  created () {
    let vm = this
    vm.$axios.get('http://192.168.102.107:7980/parameter/' + 'j315')
      .then(resp => {
        vm.parameters = resp.data
      })
  }
}
</script>

<style lang="scss">
  @import './css/index.scss';
</style>
